//
//  ActiveTableViewCell.m
//  Kfangchan
//
//  Created by liran on 14/11/15.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "ActiveTableViewCell.h"

@interface ActiveTableViewCell()

@property (strong, nonatomic) BuildingActive *active;

@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *termLabel;

@end

@implementation ActiveTableViewCell

- (void)awakeFromNib {
    self.activeView.layer.borderColor = [UIColor colorWithHexString:@"#D1CCCA"].CGColor;
    self.activeView.layer.borderWidth = 1;
    self.activeView.layer.cornerRadius = 4.0;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark -
#pragma mark 封装cell
- (void)initCell:(BuildingActive *)entity {
    if (entity) {
        self.active = entity;
        
        self.nameLabel.text = self.active.name;
        int longTerm = self.active.longTerm;
        if (longTerm == 0) {
            NSString *startDay = self.active.startDay;
            NSString *endDay = self.active.endDay;
            if (startDay == nil || startDay.length == 0) {
                startDay = @"";
            } else {
                startDay =  [startDay componentsSeparatedByCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]][0];
            }
            if (endDay == nil || endDay.length == 0) {
                endDay = @"";
            } else {
                endDay =  [endDay componentsSeparatedByCharactersInSet:[NSCharacterSet  whitespaceAndNewlineCharacterSet]][0];
            }
            
            self.termLabel.text = [NSString stringWithFormat:@"%@至%@", startDay, endDay];
        } else {
            self.termLabel.text = @"长期有效";
        }
        
        self.descrWebView.scrollView.scrollEnabled = NO;
        NSString *html = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=100%%, initial-scale=1, user-scalable=no\" /><style>body{padding:8px;margin:0;} img{width:100%%}</style></head><body>%@</body><html>", self.active.descr];
        [self.descrWebView loadHTMLString:html baseURL:nil];
    }
}

- (void)resizeCell {
    self.activeView.frame = CGRectAddHeight(self.activeView.frame, self.active.webViewHeight);
    self.descrWebView.size = CGSizeMake(self.descrWebView.width, self.active.webViewHeight);
}

@end
